package com.example.driving.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.example.driving.R;
import com.example.driving.base.BaseContext;
import com.example.driving.base.BaseFragment;
import com.example.driving.base.Data;
import com.example.driving.databinding.FragmentRegisterBinding;
import com.example.driving.model.UserBean;
import com.example.driving.ui.MainActivity;
import com.example.driving.ui.TabMineFragment;
import com.example.driving.utils.NetUtils;

;import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class RegisterFragment extends BaseFragment<FragmentRegisterBinding> {


    private FragmentRegisterBinding mBinding;


    @Override
    public FragmentRegisterBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        mBinding = FragmentRegisterBinding.inflate(inflater, container, false);
        return mBinding;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mBinding.button.setOnClickListener(view -> {
            UserBean userBean = new UserBean();
            String account = mBinding.editText.getText().toString();
            System.out.println(account);
            String password = mBinding.editText2.getText().toString();
            String email = mBinding.editText3.getText().toString();
            String code = mBinding.editText4.getText().toString();

            if (TextUtils.isEmpty(account)){
                mBinding.editText.setError("账户不能为空");
                return;
            }

            if (TextUtils.isEmpty(password)){
                mBinding.editText2.setError("密码不能为空");
                return;

            }

            if (TextUtils.isEmpty(email)){
                mBinding.editText3.setError("邮箱不能为空");
                return;


            }

            if (TextUtils.isEmpty(code)){
                mBinding.editText4.setError("内测码不能为空");
                return;


            }

            userBean.setUserAccount(account);

            userBean.setPassword(password);

            userBean.setEmail(email);

            userBean.setCode(code);


            System.out.println(userBean.toString());

            Call<Data<UserBean>> register = NetUtils.get().register(userBean);

            register.enqueue(new Callback<Data<UserBean>>() {
                @Override
                public void onResponse(Call<Data<UserBean>> call, Response<Data<UserBean>> response) {
                    Data<UserBean> body = response.body();
                    UserBean data = body.getData();

                    if (data != null) {
                        System.out.println(data.getDescription());

                        TabMineFragment.userBean = data;
                        BaseContext.setCurrentId(data.getId());
                        Intent startIntent = new Intent(getContext(), MainActivity.class);
                        startActivity(startIntent);
                    }else {
                        String description = response.body().getDescription();
                        System.out.println(description);
                        mBinding.editText.setError(description);
                    }
                }

                @Override
                public void onFailure(Call<Data<UserBean>> call, Throwable t) {

                }


            });
    });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onBaseClick(View v) {

    }

//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//        View root = inflater.inflate(R.layout.fragment_register, container, false);
//        return root;
//    }


}